package com.archstoday.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.archstoday.BaseApp;
import com.archstoday.R;
import com.archstoday.activity.MainActivity;
import com.archstoday.activity.SearchActivity;
import com.archstoday.adapter.ViewPagerAdapter;
import com.archstoday.utils.ToolUtils;
import com.archstoday.utils.WechatUtils;

import java.util.HashMap;
import java.util.Map;

public class HomeFragment extends AsBaseFragment {

    private TextView mMainSchemeTv, mMainNewTv, mMainTenderTv, mMainEngineerTv;
    private View mMainSchemeLine, mMainNewLine, mMainTenderLine, mMainEngineerLine;
    private ViewPager mViewPager;
    private ViewPagerAdapter mPagerAdapter;

    private MainTenderFragment mTenderFragment;
    private MainNewFragment mMainNewFragment;
    private MainEngineerFragment mEgFragment;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        mMainSchemeTv = (TextView) view.findViewById(R.id.main_scheme_tv);
        mMainNewTv = (TextView) view.findViewById(R.id.main_new_tv);
        mMainTenderTv = (TextView) view.findViewById(R.id.main_tender_tv);
        mMainEngineerTv = (TextView) view.findViewById(R.id.main_engineer_tv);
        mMainSchemeLine = view.findViewById(R.id.main_scheme_line);
        mMainNewLine = view.findViewById(R.id.main_new_line);
        mMainTenderLine = view.findViewById(R.id.main_tender_line);
        mMainEngineerLine = view.findViewById(R.id.main_engineer_line);
        mViewPager = (ViewPager) view.findViewById(R.id.main_pager);

        return view;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        WechatUtils.regToWx(mAct);
        mTenderFragment = new MainTenderFragment();
        mEgFragment = new MainEngineerFragment();
        mMainNewFragment = new MainNewFragment();
        mPagerAdapter = new ViewPagerAdapter(mAct.getSupportFragmentManager());
        mPagerAdapter.addFragment(new MainSchemeFragment());
        mPagerAdapter.addFragment(mMainNewFragment);
        mPagerAdapter.addFragment(mTenderFragment);
        mPagerAdapter.addFragment(mEgFragment);
        mViewPager.setAdapter(mPagerAdapter);

        selectMain(0);
        mViewPager.setCurrentItem(0);
    }

    public void onBack() {
        if (mViewPager.getCurrentItem() == 1) {
            mTenderFragment.onBack();
        } else if (mViewPager.getCurrentItem() == 2) {
            mEgFragment.onBack();
        } else {
            ((MainActivity) mAct).onBack();
        }
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.search_btn);
        setOnClickListener(R.id.main_scheme_btn);
        setOnClickListener(R.id.main_new_btn);
        setOnClickListener(R.id.main_tender_btn);
        setOnClickListener(R.id.main_engineer_btn);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectMain(position);
                Map<String, String> info;
                switch (position) {
                    case 0:
                        info = new HashMap<>();
                        info.put("name", "solution");

                        getEvent(BaseApp.E_HOME, BaseApp.PAGE_TYPE_HOME,
                                ToolUtils.mapToJson(info));
                        break;
                    case 1:
                        info = new HashMap<>();
                        info.put("name", "ratednews");

                        getEvent(BaseApp.E_HOME, BaseApp.PAGE_TYPE_HOME,
                                ToolUtils.mapToJson(info));
                        break;
                    case 2:
                        info = new HashMap<>();
                        info.put("name", "recruit");

                        getEvent(BaseApp.E_HOME, BaseApp.PAGE_TYPE_HOME,
                                ToolUtils.mapToJson(info));
                        break;
                    case 3:
                        info = new HashMap<>();
                        info.put("name", "practice");

                        getEvent(BaseApp.E_HOME, BaseApp.PAGE_TYPE_HOME,
                                ToolUtils.mapToJson(info));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void treatClickEvent(int id) {
        Intent intent;
        switch (id) {
            case R.id.search_btn:
                intent = new Intent(mAct, SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.main_scheme_btn:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.main_new_btn:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.main_tender_btn:
                mViewPager.setCurrentItem(2);
                break;
            case R.id.main_engineer_btn:
                mViewPager.setCurrentItem(3);
                break;
        }
    }

    private void selectMain(int index) {
        switch (index) {
            case 0:
                mMainSchemeTv.setSelected(true);
                mMainNewTv.setSelected(false);
                mMainTenderTv.setSelected(false);
                mMainEngineerTv.setSelected(false);

                mMainSchemeLine.setVisibility(View.VISIBLE);
                mMainNewLine.setVisibility(View.GONE);
                mMainTenderLine.setVisibility(View.GONE);
                mMainEngineerLine.setVisibility(View.GONE);
                break;
            case 1:
                mMainSchemeTv.setSelected(false);
                mMainNewTv.setSelected(true);
                mMainTenderTv.setSelected(false);
                mMainEngineerTv.setSelected(false);

                mMainSchemeLine.setVisibility(View.GONE);
                mMainNewLine.setVisibility(View.VISIBLE);
                mMainTenderLine.setVisibility(View.GONE);
                mMainEngineerLine.setVisibility(View.GONE);
                break;
            case 2:
                mMainSchemeTv.setSelected(false);
                mMainNewTv.setSelected(false);
                mMainTenderTv.setSelected(true);
                mMainEngineerTv.setSelected(false);

                mMainSchemeLine.setVisibility(View.GONE);
                mMainNewLine.setVisibility(View.GONE);
                mMainTenderLine.setVisibility(View.VISIBLE);
                mMainEngineerLine.setVisibility(View.GONE);
                break;
            case 3:
                mMainSchemeTv.setSelected(false);
                mMainNewTv.setSelected(false);
                mMainTenderTv.setSelected(false);
                mMainEngineerTv.setSelected(true);

                mMainSchemeLine.setVisibility(View.GONE);
                mMainNewLine.setVisibility(View.GONE);
                mMainTenderLine.setVisibility(View.GONE);
                mMainEngineerLine.setVisibility(View.VISIBLE);
                break;
        }
    }
}